home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / dev / gcc / ixemul_src.lha / ixemul-41.0 / library / __load_seg.c < prev    next >
C/C++ Source or Header  |  1995-05-28  |  10KB  |  414 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __load_seg.c,v 1.1.1.1 1994/04/04 04:30:54 amiga Exp
  20.  *
  21.  *  __load_seg.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:54  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.5  1992/09/14  01:38:35  mwild
  26.  *  fix a bug with #! expansion (forgot separating /)
  27.  *
  28.  *  Revision 1.4  1992/08/09  20:37:07  amiga
  29.  *  change to use 2.x header files by default
  30.  *
  31.  *  Revision 1.3  1992/07/04  19:02:39  mwild
  32.  *  fix typo, the buffer for interpreter-expansion was ways too small...
  33.  *
  34.  * Revision 1.2  1992/05/22  01:45:00  mwild
  35.  * rewrote interpreter expansion, `should' now work as expected
  36.  *
  37.  * Revision 1.1  1992/05/14  19:55:40  mwild
  38.  * Initial revision
  39.  *
  40.  */
  41.  
  42. #define KERNEL
  43. #include "ixemul.h"
  44. #include "kprintf.h"
  45.  
  46. #include <ctype.h>
  47. #include <string.h>
  48.  
  49. #if __GNUC__ != 2
  50. #define alloca __builtin_alloca
  51. #endif
  52.  
  53. /* until sksh is fixed... */
  54. #define BIG_KLUDGE
  55.  
  56.  
  57. /* 2.0 support */
  58. #include <utility/tagitem.h>
  59. #include <dos/dostags.h>
  60.  
  61. extern void *kmalloc (size_t size);
  62.  
  63. struct my_seg {
  64.   BPTR    segment;    /* the thing our clients can use */
  65.   enum { LOADSEG, RESSEG } type;
  66.   u_int    priv;        /* information depending on type */
  67. };
  68.  
  69.  
  70. static struct my_seg *try_load_seg (BPTR lock, char *name, char **args);
  71.  
  72.  
  73. static inline struct my_seg *
  74. check_resident (char *tmp)
  75. {
  76.   struct my_seg *res = 0;
  77.   struct Segment *seg = 0;
  78.  
  79.       /* big problem: Commo only stores the bare names in the resident
  80.          list. So we have to truncate to the filename part, and so lose
  81.          the ability to explicitly load the disk version even if a 
  82.          resident version is installed */
  83.  
  84.       char *cp = rindex (tmp, '/');
  85.       if (cp)
  86.     tmp = cp + 1;
  87.       else if (cp = index (tmp, ':'))
  88.     tmp = cp + 1;
  89.  
  90.       Forbid ();
  91.       seg = FindSegment (tmp, 0, 0);
  92.       if (seg)
  93.         {
  94.           /* strange they didn't provide a function for this... */
  95.           if (seg->seg_UC >= 0) 
  96.         seg->seg_UC++;
  97.     }
  98.       Permit ();
  99.  
  100.   if (seg && (res = (struct my_seg *) kmalloc (sizeof (*res))))
  101.     {
  102.       res->segment = seg->seg_Seg;
  103.       res->type    = RESSEG;
  104.       res->priv    = (u_int) seg;
  105.     }
  106.   else if (seg)
  107.     {
  108.       Forbid ();
  109.       if (seg->seg_UC > 0)
  110.     seg->seg_UC--;
  111.       Permit ();
  112.     }
  113.  
  114.   return res;
  115. }
  116.  
  117.  
  118. static inline struct my_seg *
  119. check_loadseg (char *tmp)
  120. {
  121.   struct my_seg *res = 0;
  122.   BPTR seg;
  123.   
  124.   seg = LoadSeg (tmp);
  125.   if (seg && (res = kmalloc (sizeof (*res))))
  126.     {
  127.       res->segment = seg;
  128.       res->type       = LOADSEG;
  129.       res->priv    = seg;
  130.     }
  131.   else if (seg)
  132.     UnLoadSeg (seg);
  133.     
  134.   return res;
  135. }
  136.  
  137.  
  138. void
  139. __free_seg (BPTR *seg)
  140. {
  141.   struct my_seg *ms;
  142.   
  143.   ms = (struct my_seg *) seg;
  144.   
  145.   if (ms->type == RESSEG)
  146.     {
  147.       struct Segment *s = (struct Segment *) ms->priv;
  148.  
  149.       Forbid ();
  150.       if (s->seg_UC > 0)
  151.     s->seg_UC--;
  152.       Permit ();
  153.     }
  154.   else
  155.     UnLoadSeg (ms->priv);
  156.  
  157.   kfree (ms);
  158. }
  159.  
  160.  
  161. /*
  162.  * This function does what LoadSeg() does, and a little bit more ;-)
  163.  * Besides walking the PATH of the user, we try to do interpreter expansion as
  164.  * well. But, well, we do it a little bit different then a usual Amiga-shell.
  165.  * We check the magic cookies `#!' and `;!', and if found, run the interpreter
  166.  * specified on this first line of text. This does *not* depend on any script
  167.  * bit set!
  168.  * If this check is negative, the script bit is tested. If set, the special
  169.  * BPTR (-2) is returned, this is the hint to ssystem() to go and invoke
  170.  * system() with the command line. In this case *NO* interpreter expansion
  171.  * takes place, as the expansion must have already failed before.
  172.  */
  173.  
  174. /*
  175.  * IMPORTANT: only call this function with all signals masked!!! 
  176.  */
  177.  
  178. /*
  179.  * name:        the name of the command to load. Can be relative to installed PATH
  180.  * args:        if set, a string to the first part of an expanded command is stored
  181.  */
  182.  
  183. BPTR *
  184. __load_seg (char *name, char **args)
  185. {
  186.   BPTR lock, parent_lock;
  187.   struct my_seg *seg;
  188.   char *base_name;
  189.   
  190.   /* perhaps the name is vanilla enough, so that even LoadSeg() groks it? */
  191.   if (args) *args = 0;
  192.  
  193.   seg = check_resident (name);
  194.  
  195.   if (! seg)
  196.     seg = check_loadseg (name);
  197.  
  198.   if (seg)
  199.     return &seg->segment;
  200.  
  201.   /* try to lock the file (using __lock() provides full path-parsing ;-)) */
  202.  
  203.   lock = __lock (name, ACCESS_READ);
  204.   if (lock)
  205.     {
  206.       /* this is tricky.. it is legal to CurrentDir() to a file. This is what
  207.        * we do here, we try to LoadSeg("") afterwards ;-)) */
  208.       seg = try_load_seg (lock, "", args);
  209.  
  210.       __unlock (lock);
  211.     }
  212.  
  213.   /* now we may have a valid segment */
  214.   if (seg)
  215.     return &seg->segment;
  216.  
  217.   /* if the command was specified with some kind of path, for example with a
  218.    * device or a directory in it, we don't run it thru the PATH expander
  219.    */
  220.   if (strpbrk (name, ":/"))
  221.     return 0;
  222.  
  223.   /* so the command is not directly addressable, but perhaps it's in our PATH? */
  224.   {
  225.     struct Process *me = (struct Process *)((*(struct ExecBase **)4)->ThisTask);
  226.     struct CommandLineInterface *cli;
  227.  
  228.     /* but we need a valid CLI then */
  229.     if (cli = BTOCPTR (me->pr_CLI))
  230.       {
  231.     struct path_element {
  232.       BPTR    next;
  233.       BPTR     lock;
  234.     } *lock_list;
  235.  
  236.     for (lock_list = BTOCPTR (cli->cli_CommandDir);
  237.          lock_list;
  238.          lock_list = BTOCPTR (lock_list->next))
  239.       {
  240. #ifdef 0
  241.             KPRINTF(("__load_seg: trying PATH component: next = $%lx, lock = $%lx\n",
  242.                 lock_list->next, lock_list->lock));
  243. #endif
  244.  
  245.         if (seg = try_load_seg (lock_list->lock, name, args))
  246.           break;
  247.       }
  248.       }
  249.   }
  250.   
  251.   if (seg)
  252.     return &seg->segment;
  253.  
  254.   errno = ENOENT;
  255.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  256.   return 0;
  257. }
  258.  
  259. static struct my_seg *
  260. try_load_seg (BPTR lock, char *name, char **args)
  261. {
  262.   BPTR ocd;
  263.   struct my_seg *seg;
  264.  
  265.   if (args) *args = 0;
  266.   
  267.   ocd = CurrentDir (lock);
  268.   
  269.   seg = check_loadseg (name);
  270.  
  271.   /* try to do interpreter - expansion, but only if args is non-zero */
  272.   if (! seg && args)
  273.     {
  274.       int fd;
  275.       char magic[2];
  276.       struct stat stb;
  277.       
  278.       if (syscall (SYS_stat, name, &stb) == 0 && S_ISREG (stb.st_mode))
  279.     {
  280.       if ((fd = syscall (SYS_open, name, 0)) >= 0)
  281.             {
  282.           if (syscall (SYS_read, fd, magic, 2) == 2 && 
  283.               (((magic[0] == '#' || magic[0] == ';') && magic[1] == '!')
  284. #ifdef BIG_KLUDGE
  285. /* FIXME !!! */
  286.           /* I can't currently use #! expansion with sksh, since the shell
  287.            * does the same expansion again, and then doesn't seem to get
  288.            * it right anymore... this is the so far unused hidden bit,
  289.            * set it with PROTECT +h
  290.            */
  291.           || (stb.st_amode & (1 << 7))
  292. #endif
  293.                                         ))
  294.             {
  295.               char interp[MAXPATHLEN + 1], *interp_start;
  296.               int n;
  297.           
  298.               if ((n = syscall (SYS_read, fd, interp, MAXINTERP)) > 0)
  299.             {
  300.               char *cp, *colon, ch;
  301.               char *interp_path;
  302.  
  303. #ifdef BIG_KLUDGE
  304. /* FIXME */
  305.               if (stb.st_amode & (1<<7))
  306.                 {
  307.               strcpy (interp, "sksh -c");
  308.               n = strlen (interp);
  309.             }
  310. #endif
  311.  
  312.               /* oky.. got one.. terminate with 0 and try to find end of it */
  313.               interp[n] = 0;
  314.               for (interp_start = interp; isspace(*interp_start) && interp_start < interp + n; interp_start++);
  315.               for (cp = interp_start; cp < interp + n; cp++)
  316.                 if (*cp == 0 || isspace (*cp)) break;
  317.               ch = *cp;
  318.               *cp = 0;
  319.  
  320.               /* okay, lets try to load this instead. Call us recursively,
  321.                * but leave out the argument-argument, so we can't get
  322.                * into infinite recursion. Interpreter-Expansion is only
  323.                * done the first time __load_seg() is called from
  324.                * ssystem()
  325.                */
  326.               seg = (struct my_seg *) __load_seg (interp_start, 0);
  327.               *cp = ch;
  328.               if (! seg)
  329.                 goto ret;
  330.           
  331.               /* in this case, set the argument as well. 
  332.                */
  333.  
  334.               /* first skip intergap whitespace */
  335.               for (;cp < interp + n; cp++)
  336.             if (!*cp || ! isspace (*cp) || *cp == '\n')
  337.               break;
  338.  
  339.               if (*cp && *cp != '\n')
  340.                 {
  341.               /* we read a certain amount of bytes, but we 
  342.                * unconditionally stop when we hit newline
  343.                */
  344.               interp_path = cp;
  345.  
  346.                   /* crop any further arguments, only ONE argument
  347.                    * is supported
  348.                */
  349.                   for (;cp < interp + n; cp++) 
  350.                 if (isspace (*cp)) 
  351.                   break;
  352.                   if (cp < interp + n)
  353.                 *cp = 0;
  354.  
  355.               *cp++ = ' ';
  356.             }
  357.               else
  358.             cp = interp_path = interp_start;
  359. #ifdef BIG_KLUDGE
  360. /* FIXME */
  361.               if (stb.st_amode & (1<<7))
  362.                 {
  363.                   *cp++ = '\"';
  364.                 }
  365. #endif
  366.               if (ix.ix_translate_slash && name[0] != '/')
  367.             *cp++ = '/';
  368.  
  369.               if (name[0] != '/' && !index (name, ':'))
  370.             {
  371.               if (NameFromLock (lock, cp, 
  372.                         MAXPATHLEN-(cp-interp)) == -1)
  373.                             {
  374.                               if (*name)
  375.                     strcat (strcat (cp, "/"), name);
  376.                 }
  377.               else
  378.                 strcpy (cp, name);
  379.             }
  380.               else
  381.             strcpy (cp, name);
  382.  
  383.               if (ix.ix_translate_slash && (colon = index (cp, ':')))
  384.             *colon = '/';
  385.  
  386.               *args = (char *) syscall (SYS_strdup, interp_path);
  387. #ifdef BIG_KLUDGE
  388. /* FIXME */
  389.               if (stb.st_amode & (1<<7))
  390.                 {
  391.               /* tell ssystem() that it should add the closing
  392.                * quote... shudder.. I want a newer sksh !
  393.                */
  394.                   *args = (char *)(-(int)*args);
  395.                 }
  396. #endif
  397.             }
  398.             }
  399.           syscall (SYS_close, fd);
  400.         }
  401.  
  402. ret:  
  403.       /* check to see if script bit is set, no matter whether we could
  404.        * actually open the file or not. If set, set seg to magic BPTR */
  405.       if (! seg && (stb.st_amode & FIBF_SCRIPT))
  406.         seg = (struct my_seg *) -2;
  407.         }
  408.     }
  409.  
  410.   CurrentDir (ocd);
  411.   
  412.   return seg;
  413. }
  414.